<?php

namespace App\Models\Rbac;

use App\Models\BaseModel;
use App\Models\Employee;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class User extends BaseModel
{
    use HasFactory;

    protected $table = 'user';

    // 允许批量操作的字段
    protected $fillable = [
        'username',
        'password',
        'token',
        'token_expire_time',
        'status',
        'expire_time',
        'company_name',
        'last_login_time',
        'last_login_ip',
        'created_at',
        'updated_at',
        'created_at',
        'phone',
        'is_primary'
    ];

    public function scopeSearch($query,$params)
    {

        if(!empty($params['company_name'])){
            $query->where('company_name','like','%'.$params['company_name'].'%');
        }
        if(!empty($params['card_number'])){
            $user_id = Employee::where('card_number',$params['card_number'])->value('user_id');
            $query->where('id',$user_id);
        }
        $query->where('is_del',2);

        return $query;
    }

    public function role(){
        return $this->belongsToMany(Role::class,'user_role','user_id','role_id');
    }

    /**员工**/
    public function employee(){
        return $this->belongsTo(Employee::class,'id','user_id')->with('job_type');
    }



}
